﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DuJianliang.ApplicationPlatform.TempEquipment
{
    public partial class TempEquipmentList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            var dal = new TempEquipmentDal();
            grv.DataSource = dal.GetTempEquipmentDataTable(ddlFlowOperation.SelectedValue);
            grv.DataBind();
        }

        protected void grv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var dal = new TempEquipmentDal();
            switch (e.CommandName)
            {
                case "CmdShowFlow":
                    BindGrid();
                    break;
                case "modify":
                    Response.Redirect("TempEquipmentEdit.aspx?FlowInstanceID=" + e.CommandArgument.ToString() + "&Source=list");
                    break;
                case "del":
                    dal.TempEquipment_Delete(e.CommandArgument.ToString());
                    BindGrid();
                    break;
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("TempEquipmentEdit.aspx?Source=list");
        }

        protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
                return;

            string flowID = grv.DataKeys[e.Row.RowIndex]["FlowID"].ToString();
            string flowInstanceID = grv.DataKeys[e.Row.RowIndex]["FlowInstanceID"].ToString();

            TableCell tableCell_FlowCompleted = e.Row.Cells[7];
            string flowCompleted = tableCell_FlowCompleted.Text.ToLower();
            switch (flowCompleted)
            {
                case "true":
                    tableCell_FlowCompleted.Text = "已完成";
                    e.Row.BackColor = System.Drawing.Color.LightGreen;
                    break;
                case "false":
                    tableCell_FlowCompleted.Text = "处理中";
                    e.Row.BackColor = System.Drawing.Color.Pink;
                    break;
                default:
                    tableCell_FlowCompleted.Text = "无";
                    break;
            }

            Button btnFlow = e.Row.FindControl("btnFlow") as Button;
            if (btnFlow != null)
            {
                string url = string.Format("/Flow/FlowManager.aspx?FlowID={0}&FlowInstanceID={1}", flowID, flowInstanceID);
                btnFlow.Attributes.Add("onclick", string.Format("showPage('{0}');", url));
            }

            Button btnDelete = e.Row.FindControl("btnDelete") as Button;
            if (btnDelete != null)
            {
                if (Common.FlowHelper.HasFlowData(new Guid(flowInstanceID)))
                {
                    btnDelete.Visible = false;
                }
                else
                {
                    btnDelete.Visible = true;
                }
            }   
        }
        protected void ddlFlowOperation_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}